package org.stream.answerduel.service;

import com.alibaba.fastjson.JSONObject;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.stream.answerduel.util.HttpRequestUtil;
import org.stream.answerduel.vo.Subject.Option;
import org.stream.answerduel.vo.Subject.SingleSubject;
import org.stream.answerduel.vo.Subject.SubjectDetails;
import org.stream.answerduel.vo.Subject.SubjectTypeVo;

import lombok.extern.slf4j.Slf4j;

import org.stream.answerduel.vo.SubjectVo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author  郭榕
 * @date    2019/7/12 14:16
 *
 * 功能描述: 本业务对接题库系统的接口，从题库系统接口中抽取题目作为对战选题;
 *          从接口所拿到的题目用SubjectVo封装;
 *
 */

@Service
@Slf4j
public class SubjectService {

    @Value("${question.bank.api}")
    private String ip = "";

    /**
     * @author  郭榕
     * @date 2019/7/17 14:41
     * @功能描述: 从题库返回的详细题目数据封装类List<SubjectDetails>
     * 向目的URL发送get请求
     * @return  SubjectDetails
     *
     */
    public List<SubjectDetails> getSubjectDetails(){

        final String url = ip +"/api/question/get-questions?" +
                "questionCount=1" +
                "&tagIdLists=598462142978457600" +
                "&tagIdLists=598462142978457668" ;
        List<SubjectDetails> subjectLists = new ArrayList<SubjectDetails>();
        int i = 0;
        while(i < 5)
        {
            JSONObject jsonObject = JSONObject.parseObject(HttpRequestUtil.sendGetRequestToBank(url));
            String jsonStr = jsonObject.getString("data");
            System.out.println(jsonStr);
            SubjectDetails subjectDetails = JSONObject.parseArray(jsonStr,SubjectDetails.class).get(0);

            if(subjectDetails.getTopic().getContent().length() < 80)
            {
                if(checkSubjectRepeat(subjectLists , subjectDetails))
                {
                    subjectLists.add(subjectDetails);
                    i++;
                    //System.out.println(subjectDetails);
                }
               else{
                   continue;
                }
            }
        }
        return subjectLists;
    }

    public boolean checkSubjectRepeat(List<SubjectDetails> detailsList , SubjectDetails details)
    {
        for(SubjectDetails s : detailsList)
        {
            if(s.getTopic().getId().equals(details.getTopic().getId()))
            {
                return false;
            }
        }
        return true;
    }

    /**
     * @author 郭榕
     * @date 2019/7/17 14:50
     * 功能描述:得到题目类型标签tags以及对应的Id
     * @return  List<SubjectTypeVo>
     *
     */
    public List<SubjectTypeVo> getSubjectTags(){

        final String url = ip+"/api/tag/get-all-tags";
        JSONObject jsonObject = JSONObject.parseObject(HttpRequestUtil.sendGetRequestToBank(url));
        String jsonStr = jsonObject.getString("data");
        //System.out.println(jsonStr);
        List<SubjectTypeVo> subjectTypeVos = JSONObject.parseArray(jsonStr, SubjectTypeVo.class);

        return subjectTypeVos;
    }


    /**
     * @author 郭榕
     * @date 2019/7/18 14:46
     * 功能描述：根据题目id获取答案;
     * @return String
     *
     */
    public String getSubjectAnswer(String subjectId){
        final String url = ip+"/api/answer/get-answer?topicId="+subjectId;
        JSONObject jsonObject = JSONObject.parseObject(HttpRequestUtil.sendGetRequestToBank(url));
        String jsonStr = jsonObject.getJSONArray("data").get(0).toString();
        return jsonStr;
    }
    /**
     * @author 郭榕
     * @date 2019/7/17 14:27
     * @功能描述，得到随机五道题，困难，单选,并封装成SubjectVo；
     */
    public List<SubjectVo> getFiveSubject(){

        List<SubjectVo> subjectVoList = new ArrayList<>();

        for(SubjectDetails s : this.getSubjectDetails()) {
            SubjectVo subjectVo = new SubjectVo();
            subjectVo.setSubjectID(s.getTopic().getId());
            subjectVo.setSubjectDetails(s.getTopic().getContent());
            List<String> options = new ArrayList<>();
            for(Option o : s.getOptions())
            {
                options.add(o.getContent());
            }
            subjectVo.setSubjectOptions(options);
            String answer = this.getSubjectAnswer(subjectVo.getSubjectID());
            for(int i = 0 ; i<subjectVo.getSubjectOptions().size() ; i++)
            {
                if(answer.equals(subjectVo.getSubjectOptions().get(i)))
                {
                    subjectVo.setRightOption(i+1);
                    log.info("题目："+s.getTopic().getContent());
                    log.info("答案："+(i+1)+"："+answer);
                }
            }
            subjectVoList.add(subjectVo);
        }

        return subjectVoList;
    }


    /**
     * @author  郭榕
     * @date 2019/7/22 11:30
     * 根据题目id获取题目信息;
     */
    public SubjectVo getSubjectBySubjectId(String subjectId){
        final String url = ip+"/api/question/get-topicDetail?topicId="+ subjectId;
        JSONObject jsonObject = JSONObject.parseObject(HttpRequestUtil.sendGetRequestToBank(url));
        String jsonStr = jsonObject.getString("data");
        //System.out.println(jsonStr);
        SingleSubject singleSubject = JSONObject.parseObject(jsonStr, SingleSubject.class);
        //System.out.println(singleSubject);
        SubjectVo subjectVo = new SubjectVo();
        subjectVo.setSubjectID(singleSubject.getId());
        subjectVo.setSubjectDetails(singleSubject.getTopicContent());
        subjectVo.setSubjectOptions(singleSubject.getOptions());
        for(int i = 0 ; i<subjectVo.getSubjectOptions().size() ; i++)
        {
            if(singleSubject.getAnswer().get(0).equals(subjectVo.getSubjectOptions().get(i)))
            {
                subjectVo.setRightOption(i+1);
            }
        }
        return subjectVo;
    }

}
